/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.plattysoft.leonids.examples.slice;

import com.plattysoft.leonids.examples.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

public class ExampleListAbilitySlice extends AbilitySlice {
    private ComponentContainer mRootId;
    String[] list = {"One Shot Simple"/*, "One Shot Advanced (Not Implemented)"*/
            , "Emiter Simple"
            , "Emiting on background [NEW]"
            , "Emiter Intermediate"
            , "Emiter Time Limited"
            , "Emit with Gravity [NEW]"
            , "Follow touch [NEW]"
            , "Animated particles"
            , "Fireworks"
            , "Confetti [Rabbit and Eggs]"
            , "Dust [Rabbit and Eggs]"
            , "Stars [Rabbit and Eggs]"
//				, "Animated Particles"
    };
    private ListContainer list_component;
    private ComponentContainer root_layout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        root_layout = (ComponentContainer) LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_main_list, null, false);
        mRootId = (ComponentContainer) root_layout.findComponentById(ResourceTable.Id_parentid);
        ShapeElement background_color_element = new ShapeElement();
        background_color_element.setShape(ShapeElement.RECTANGLE);
        background_color_element.setRgbColor(new RgbColor(0, 0, 0));
        background_color_element.setBounds(0, 0, mRootId.getRight(), mRootId.getBottom());
        mRootId.setBackground(background_color_element);
        super.setUIContent(root_layout);
        initList();
    }

    private void initList() {
        list_component = (ListContainer) root_layout.findComponentById(ResourceTable.Id_lc_main);
        list_component.setItemProvider(new MainListProvider(this, list));
        list_component.setItemClickedListener((listContainer, component, i, l) -> {
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
            switch (i) {
                case 0:
                    present(new OneShotSimpleExampleAbilitySlice(), intent);
                    break;
                case 1:
                    present(new EmiterSimpleExampleAbilitySlice(), intent);
                    break;
                case 2:
                    present(new EmiterBackgroundSimpleExampleAbilitySlice(), intent);
                    break;
                case 3:
                    present(new EmiterIntermediateExampleAbilitySlice(), intent);
                    break;
                case 4:
                    present(new EmiterTimeLimitedExampleAbilitySlice(), intent);
                    break;
                case 5:
                    present(new EmiterWithGravityExampleAbilitySlice(), intent);
                    break;
                case 6:
                    present(new FollowCursorExampleAbilitySlice(), intent);
                    break;
                case 7:
                    present(new AnimatedParticlesExampleAbilitySlice(), intent);
                    break;
                case 8:
                    present(new FireworksExampleAbilitySlice(), intent);
                    break;
                case 9:
                    present(new ConfettiExampleAbilitySlice(), intent);
                    break;
                case 10:
                    present(new DustExampleAbilitySlice(), intent);
                    break;
                case 11:
                    present(new StarsExampleAbilitySlice(), intent);
                    break;
            }
        });
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
        initList();
    }

    public class MainListProvider extends BaseItemProvider {
        String[] list_items;
        Context context;

        MainListProvider(Context ctx, String[] lst) {
            list_items = lst;
            context = ctx;
        }

        @Override
        public int getCount() {
            return list_items.length;
        }

        @Override
        public Object getItem(int i) {
            return list_items[i];
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
            if (component == null) {
                component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_list_item, componentContainer, false);
            }
            ((Button) (component.findComponentById(ResourceTable.Id_list_component))).setText((String) getItem(i));
            component.setClickable(false);
            return component;
        }
    }
}
